package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.entity.ReportRisk;
import com.simon.annual.repositoy.IReportRiskRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;
import com.simon.annual.service.IDeclarationPrompt;

/**
 * 第一节：处理声明与提示
 * */
@Service
public class FindDeclarationPromptImpl implements IDeclarationPrompt{
	
	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportRiskRepositoy reportRiskRepositoy;
	@Resource
	private Environment env;
	

	@Override
	public ReportNeeq analysis(Document doc, ReportNeeq reportNeeq,int companyPtrId) {
		
		Elements ele = doc.getElementsMatchingOwnText(env.getProperty("pdf.one"));
		if(ele.size()>0){
			Element index =ele.first();
			for (Element element : ele) {
				if(element.parent().tagName().equals("taggedpdf-doc")){
					Element yes = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.one.shf"), element,-5);
					if(yes!=null){
						index = yes;
						break;
					}
				}
			}
			Elements trs=index.select("tr");
			//不含标题
			if(trs.size()==3){
				reportNeeq.setHasManagementDisagreement(trs.get(0).children().last().text());
				reportNeeq.setHasUnattendedPrinciple(trs.get(1).children().last().text());
				reportNeeq.setHasDisclosureExemption(trs.get(2).children().last().text());
			}
			//含标题
			if(trs.size()==4){
				reportNeeq.setHasManagementDisagreement(trs.get(1).children().last().text());
				reportNeeq.setHasUnattendedPrinciple(trs.get(2).children().last().text());
				reportNeeq.setHasDisclosureExemption(trs.get(3).children().last().text());
			}
			Element x = index.nextElementSibling();
			if(x!=null){
				//1、董事、监事、高级管理人员对年度报告内容存在异议或无法保证的理由（如有）
				Element disagreementReview = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.one.yy"), x,0);
				if(disagreementReview!=null){
					reportNeeq.setManagementDisagreementReview(disagreementReview.nextElementSibling().text());
				}
				Element unattendedPrincipleReview = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.one.wcx"), x,-10);
				if(unattendedPrincipleReview!=null){
					reportNeeq.setUnattendedPrincipleReview(unattendedPrincipleReview.nextElementSibling().text());
				}
				Element disclosureExemptionReview = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.one.pl"), x,-30);
				if(disclosureExemptionReview!=null){
					reportNeeq.setDisclosureExemptionReview(disclosureExemptionReview.nextElementSibling().text());
				}
				//重要风险提示
				List<ReportRisk> riskList = new ArrayList<ReportRisk>();
				Element table = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.one.zyfxmc"), x,-45);
				if(table!=null){
					table = cmpanyPublicInfo.getNextTag(table,table);
					table = buildMeet(table);
					Elements tr = table.select("tr");
					for (int i = 1; i < tr.size()-1; i++) {
						ReportRisk risk = new ReportRisk();
						Elements td = tr.get(i).children();
						if(td.size()==2){
							risk.setName(td.get(0).text());
							risk.setDescription(td.get(1).text());
							risk.setProjectScopeId(companyPtrId);
							riskList.add(risk);
						}
						reportNeeq.setIsMajorRiskChanged(tr.last().text());
					}
					reportRiskRepositoy.save(riskList);
				}
			}
		}
		return reportNeeq;
	}

	/**
	 * 合并table换行
	 * */
	private Element buildMeet(Element newElement){
		Elements tbodys = newElement.select("tbody");
		//合并分页table
		if(tbodys.size()>0){
			for (int i = 0; i < tbodys.size()-1; i++) {
				Element body = tbodys.get(i+1);
				//去除多余的合并内容
				if(body.getElementsMatchingOwnText(env.getProperty("pdf.two")).hasText()){
					while(i+1<tbodys.size()){
						tbodys.get(i+1).remove();
						i++;
					}
					return newElement;
				}
				Elements  element = body.select("tr");
				if(!element.isEmpty()){
					boolean ps = element.first().children().size()==tbodys.get(i).select("tr").last().children().size();
					if(ps&&!body.select("tr").first().children().first().hasText()){
						Elements eTd = element.first().children();
						for (int j = 0; j < eTd.size(); j++) {
							tbodys.get(i).select("tr").last().child(j).appendText(eTd.get(j).text());
						}
						element.first().remove();
						if(!tbodys.get(i+1).hasText()){
							tbodys.remove(i+1);
						}
					}
				}
			}
		}
		return newElement;
	}
}
